﻿#'
#' @TODO :  this is tittle
#' @title : # this is tittle
#' @description: 
#' @details : firse line
#' @param : 
#' @param 1: 
#' @param 2: 
#' @param 3: 
#' @param 4: 
#' @param 5: 
#' @param 6: 
#' @param 7: 
#' @param 8: 
#' @param 9: 
#' @param seed: 
#' @param saveplot: 
#' @return:  a list
#' @examples: 
#' @author: *WYK*
#'


SankeyDiagram <- function(Data = NULL, ...) {
  library(ggplot2)
  library(ggalluvial)
  library(RColorBrewer)

  data_for_alluvial <- inner_join(train_clin_os, training[[3]]) %>%
    inner_join(., clin_2) %>%
    select(riskgroup, cluster, status, IPI) %>%
    mutate(status = ifelse(status == 1, "Dead", "Alive")) %>%
    rename(Score = riskgroup) %>%
    select(cluster, everything())


  data_for_alluvial$Freq <- 1 # 定义纵坐标，一般默认为1
  data_for_alluvial_long <- to_lodes_form(data_for_alluvial,
    axes = 1:4, # 将miRNA和mRNA分别编号
    id = "Cohort"
  ) # 改为长数据便于画图
  head(data_for_alluvial_long)

  ########### geom_flow
  alluvial_p <- ggplot(
    data_for_alluvial_long,
    aes(x = factor(x), y = Freq, stratum = stratum, alluvium = Cohort, fill = stratum, label = stratum)
  ) +
    scale_x_discrete(limits = c()) + # 去掉横坐标轴
    geom_stratum() +
    theme_bw(15) + # 定义主题
    geom_flow(width = 1 / 3, knot.pos = 1 / 4) + # 画流动图
    geom_text(stat = "stratum", size = 4) + # 添加名字
    theme(
      legend.position = "none",
      axis.title = element_blank(),
      axis.text.y = element_blank(),
      panel.grid.major = element_blank(),
      # panel.grid.minor  = element_blank(),
      panel.border = element_blank(),
      axis.ticks.y.left = element_blank(),
      axis.ticks.x.bottom = element_blank()
    ) +
    scale_fill_manual(values = c(brewer.pal(8, "Set2"), brewer.pal(8, "Set3")))
  alluvial_p
}
